﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout_lay_admin.cshtml";
}

<style type="text/css">
    /*权限*/
    .permission-list {
        border: solid 1px #eee;
    }

        .permission-list > dt {
            background-color: #efefef;
            padding: 5px 10px
        }

        .permission-list > dd {
            padding: 10px;
            padding-left: 30px
        }

            .permission-list > dd > dl {
                border-bottom: solid 1px #eee;
                padding: 5px 0
            }

                .permission-list > dd > dl > dt {
                    display: inline-block;
                    float: left;
                    white-space: nowrap;
                    width: 100px
                }

                .permission-list > dd > dl > dd {
                    margin-left: 100px;
                }

                    .permission-list > dd > dl > dd > label {
                        padding-right: 10px
                    }
</style>
<div class="layuimini-container">
    <div class="layuimini-main">
        @*<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>用户角色</legend>
            </fieldset>*@
        <div class="layui-row">

            <div class="layui-col-xs3" style=" border:1px solid #00A39A;">
                <div id="test9" class="demo-tree demo-tree-box" style="width: 100%; height: 100%; "></div>
            </div>
            <div class="layui-col-xs9">
                <div style="width:100%;height:100%;border:1px solid #00A39A;margin-left:4px;">
                    <div style="margin: 10px 10px 10px 10px">
                        <input type="text" value="" style="display:none" id="Role_id" />
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">角色名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="rolename" id="rolename" autocomplete="off" value="市场部经理" class="layui-input" />
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input" />
                            </div>
                        </div>
                    </div>
                    <div>
                        <label class="form-label col-xs-4 col-sm-2">角色功能：</label>
                        <div class="formControls col-xs-8 col-sm-9" id="MenuAction">
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label"></label>
                            <button class="layui-btn" lay-submit="" lay-filter="setmypass" id="btn_Save">提交修改</button>
                        </div>
                    </div>
                </div>
            </div>

        </div>


    </div>
</div>

<script>


    //接收暂存  子窗口调用
    var addnoewfunction;
    //当前页面全局变量
    var tree;

    var nodeName = "";

    var treeloadload = function () {

        $.ajax({
            type: "GET",
            url: '/Base_Role/Search',
            dataType: 'json',
            success: function (_data) {
                //debugger
                //, url: '/Base_Role/Search'
                //开启节点操作图标
                tree.render({
                    elem: '#test9'
                    , data: _data
                    , edit: ['add', 'del'] //操作节点的图标
                    , click: function (obj) {
                        //layer.msg(obj.data.id + "," + obj.data.title + "," + obj.data.attrName);
                        $("#rolename").val(obj.data.title);
                        $("#Role_id").val(obj.data.id);
                        $.ajax({
                            url: '/Base_Role/GetButtonAction',
                            type: 'get',
                            dataType: 'text',
                            data: { primarykey: obj.data.id },
                            success: function (resp) {

                                var json = eval('(' + resp + ')');
                                //var index = layer.alert(json.Message, function () {
                                //});
                                for (var i = 0; i < resp.length; i++) {

                                }

                                $('input[name="fFuncId"],input[name="fMenu"]').each(function (index, item) {
                                    //$(this).removeAttr("checked");
                                    $(this).prop("checked", false);
                                });

                                $('input[name="fFuncId"]').each(function (index, item) {
                                    for (var i = 0; i < json.length; i++) {
                                        if (json[i] == $(this).val()) {
                                            $(this).prop("checked", 'true');
                                            $(this).closest("dl").find("dt input:checkbox").prop("checked", true);
                                            $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", true);
                                            break;
                                        }
                                    }

                                });

                            },
                            error: function (resp) {
                                layer.alert("对不起出错了");
                            }
                        });

                    },
                    text: {
                        defaultNodeName: nodeName //节点默认名称
                        , none: '' //数据为空时的提示文本
                    },
                    limitNodeAddLevel: 0
                    , customOperate: true
                    , operate: function (obj, callback) {
                        var type = obj.type; //得到操作类型：add、edit、del
                        var data = obj.data; //得到当前节点的数据
                        var elem = obj.elem; //得到当前节点元素
                        //layer.msg(type);
                        var id = data.id; //得到节点索引
                        if (type == "add") {
                            addnoewfunction = callback;
                            layer.open({
                                title: '添加角色',
                                type: 2,
                                shade: 0.2,
                                maxmin: true,
                                shadeClose: true,
                                area: ['700px', '500px'],
                                content: '/Base_Role/Create?groupselect=' + id,
                                success: function (layero, index) {
                                }
                            });
                        } else if (type == "del") {
                            $.ajax({
                                url: '/Base_Role/ActivateSwich',
                                type: 'post',
                                dataType: 'text',
                                data: { primarykeys: id },
                                success: function (resp) {
                                    var json = eval('(' + resp + ')');
                                    //var index = layer.alert(json.Message, function () {
                                    //});
                                    callback();
                                },
                                error: function (resp) {
                                    layer.alert("对不起出错了");
                                }
                            });

                        } else if (type == "update") {
                            addnoewfunction = callback;
                            layer.open({
                                title: '修改角色',
                                type: 2,
                                shade: 0.2,
                                maxmin: true,
                                shadeClose: true,
                                area: ['700px', '500px'],
                                content: '/Base_Role/Edit?roleid=' + id,
                                success: function (layero, index) {
                                }
                            });
                        }
                        //layer.msg(id);

                    }
                });
            }
        });

        var index0pen;

    }


    //$.ajax({
    //           url: '/Base_Role/Create',
    //           type: 'post',
    //           dataType: 'text',
    //           data: {UserId:"",Name:""},
    //           success: function (resp) {
    //               var json = eval('(' + resp + ')');
    //               var index = layer.alert(json.Message, function () {
    //                   // 关闭弹出层
    //                   layer.close(index);
    //                   parent.reloadTable();
    //                   var iframeIndex = parent.layer.getFrameIndex(window.name);
    //                   parent.layer.close(iframeIndex);
    //               });
    //               //layer.alert(json.Message);
    //               //reloadTable();
    //           },
    //           error: function (resp) {
    //               layer.alert("对不起出错了");
    //           }
    //       });


    layui.use(['tree', 'util'], function () {
        window.tree = layui.tree;
        var layer = layui.layer;
        var util = layui.util;

        treeloadload();



        //页面check加载
        $.ajax({
            url: '/Base_Button/MenuAction',
            type: 'get',
            dataType: 'text',
            //data: { primarykeys: id },
            success: function (resp) {
                var json = eval('(' + resp + ')');
                var $MenuAction = $("#MenuAction");
                var str = '';
                //return;

                for (var l = 0; l < json.length; l++) {
                    str += '<dl class="permission-list">';
                    var rows = json[l];
                    str += '<dt><label><input type="checkbox" value="' + rows.NodeId + '" name="fMenu" checked="">' + rows.NodeName + '</label></dt><dd>';

                    if (rows.Children != null) {
                        for (var c = 0; c < rows.Children.length; c++) {
                            str += '<dl class="cl permission-list2">';
                            str += '<dt><label class=""><input type="checkbox" value="' + rows.Children[c].NodeId + '" name="fFuncId" checked=""/>' + rows.Children[c].NodeName + '</label></dt>';
                            str += '<dd>';
                            for (var b = 0; b < rows.Children[c].Buttons.length; b++) {
                                str += '<label class=""><input type="checkbox" value="' + rows.Children[c].Buttons[b].Id + '" name="fFuncId" checked=""/>' + rows.Children[c].Buttons[b].Name + '</label>';
                            }
                            str += ' </dd>';
                            str += '</dl>';
                        }
                    }
                    str += '</dd></dl>';
                }

                $MenuAction.html(str);

                init();
            },
            error: function (resp) {
                $MenuAction.html("");
                layer.alert("对不起出错了");
            }
        });

        //check初始化
        var init = function () {
            //$('.skin-minimal input').iCheck({
            //    checkboxClass: 'icheckbox-blue',
            //    radioClass: 'iradio-blue',
            //    increaseArea: '20%'
            //});
            $(".permission-list dt input:checkbox").click(function () {
                $(this).closest("dl").find("dd input:checkbox").prop("checked", $(this).prop("checked"));
            });
            $(".permission-list2 dd input:checkbox").click(function () {
                var l = $(this).parent().parent().find("input:checked").length;
                var l2 = $(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
                if ($(this).prop("checked")) {
                    $(this).closest("dl").find("dt input:checkbox").prop("checked", true);
                    $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", true);
                }
                else {
                    if (l == 0) {
                        $(this).closest("dl").find("dt input:checkbox").prop("checked", false);
                    }
                    if (l2 == 0) {
                        $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", false);
                    }
                }
            });
        }

        //提交保存
        $("#btn_Save").click(function () {
            var str = "";
            var chk_value = [];
            $('input[name="fFuncId"]:checked').each(function () {
                chk_value.push($(this).val());
            });
            var _role_id = $("#Role_id").val();


            $.ajax({
                url: '/Base_Role/SaveButtonAction',
                type: 'post',
                dataType: 'text',
                data: { primarykeys: chk_value.toString(), roleid: _role_id },
                success: function (resp) {
                    var json = eval('(' + resp + ')');
                    layer.alert(json.Message);
                },
                error: function (resp) {
                    layer.alert("对不起出错了");
                }
            });

        });



    });
</script>
